package com.chancingpack.util;

import org.apache.commons.lang.time.DateUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CookieUtils {
 
    
 
    /**
     * 添加一个新Cookie
     * 
     * @author zifangsky
     * @param response
     *            HttpServletResponse
     * @param cookie
     *            新cookie
     * 
     * @return null
     */
    public static void addCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null){
            Date date = DateUtils.addMonths(new Date(), 12);
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
            StringBuilder builder = new StringBuilder();
            builder.append(cookie.getName() + "=" + cookie.getValue() + "; ");
            builder.append("Path=/; HttpOnly; ");
            builder.append("Expires=" + sdf.format(date));
            response.setHeader("Set-Cookie", builder.toString());
            response.setHeader("P3P","CP=\"NON DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAa IVDa CONa HISa TELa OTPa OUR UNRa IND UNI COM NAV INT DEM CNT PRE LOC\"");
           // response.addCookie(cookie);
        }

    }
 
    /**
     * 添加一个新Cookie
     * 
     * @author zifangsky
     * @param response
     *            HttpServletResponse
     * @param cookieName
     *            cookie名称
     * @param cookieValue
     *            cookie值
     * @param domain
     *            cookie所属的子域
     * @param httpOnly
     *            是否将cookie设置成HttpOnly
     * @param maxAge
     *            设置cookie的最大生存期
     * @param path
     *            设置cookie路径
     * @param secure
     *            是否只允许HTTPS访问
     * 
     * @return null
     */
    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain,
            boolean httpOnly, int maxAge, String path, boolean secure) {
        if (cookieName != null && !cookieName.equals("")) {
            if (cookieValue == null)
                cookieValue = "";
 
            Cookie newCookie = new Cookie(cookieName, cookieValue);
            if (domain != null)
                newCookie.setDomain(domain);
 
           // newCookie.setHttpOnly(httpOnly);
 
            if (maxAge > 0)
                newCookie.setMaxAge(maxAge);
 
            if (path == null)
                newCookie.setPath("/");
            else
                newCookie.setPath(path);
 
            newCookie.setSecure(secure);
 
            addCookie(response, newCookie);
        }
    }
 
    /**
     * 添加一个新Cookie
     * 
     * @author zifangsky
     * @param response
     *            HttpServletResponse
     * @param cookieName
     *            cookie名称
     * @param cookieValue
     *            cookie值
     * @param domain
     *            cookie所属的子域
     * 
     * @return null
     */
    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain) {
        addCookie(response, cookieName, cookieValue, domain, true, 2592000 , "/", false);
    }
 
    /**
     * 根据Cookie名获取对应的Cookie
     * 
     * @author zifangsky
     * @param request
     *            HttpServletRequest
     * @param cookieName
     *            cookie名称
     * 
     * @return 对应cookie，如果不存在则返回null
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
 
        if (cookies == null || cookieName == null || cookieName.equals(""))
            return null;
 
        for (Cookie c : cookies) {
            if (c.getName().equals(cookieName))
                return (Cookie) c;
        }
        return null;
    }
 
    /**
     * 根据Cookie名获取对应的Cookie值
     * 
     * @author zifangsky
     * @param request
     *            HttpServletRequest
     * @param cookieName
     *            cookie名称
     * 
     * @return 对应cookie值，如果不存在则返回null
     */
    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie cookie = getCookie(request, cookieName);
        if (cookie == null)
            return null;
        else
            return cookie.getValue();
    }
 
    /**
     * 删除指定Cookie
     * 
     * @author zifangsky
     * @param response
     *            HttpServletResponse
     * @param cookie
     *            待删除cookie
     */
    public static void delCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            cookie.setPath("/"); 
            cookie.setMaxAge(0);
            cookie.setValue(null);
 
            response.addCookie(cookie);
        }
    }
 
    /**
     * 根据cookie名删除指定的cookie
     * 
     * @author zifangsky
     * @param request
     *            HttpServletRequest
     * @param response
     *            HttpServletResponse
     * @param cookieName
     *            待删除cookie名
     */
    public static void delCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && c.getName().equals(cookieName)) {
            delCookie(response, c);
        }
    }
 
    /**
     * 根据cookie名修改指定的cookie
     * 
     * @author zifangsky
     * @param request
     *            HttpServletRequest
     * @param response
     *            HttpServletResponse
     * @param cookieName
     *            cookie名
     * @param cookieValue
     *            修改之后的cookie值
     * @param domain
     *            修改之后的domain值
     */
    public static void editCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
            String cookieValue,String domain) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && cookieName != null && !cookieName.equals("") && c.getName().equals(cookieName)) {
            addCookie(response, cookieName, cookieValue, domain);
        }
    }
}